<?php

namespace App\Http\Controllers\Front;

use Carbon\Carbon;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Swagger\Annotations as SWG;

class LabelController extends Controller
{
    /**
     * @SWG\Get(
     *     path="/front/articles/label",
     *     tags={"文章相关"},
     *     summary="标签",
     *     description="标签",
     *     produces={"application/json"},
     *     @SWG\Response(
     *          response="200",
     *          description="调用成功",
     *          @SWG\Schema(
     *              type="array",
     *              @SWG\Items(
     *                  @SWG\Property(property="id", type="integer", description="id"),
     *                  @SWG\Property(property="name", type="string", description="标签"),
     *              )
     *          )
     *      )
     * )
     */
    public function index()
    {
        return $this->renderJson(DB::table('article_label')->select('id', 'name')->where('is_enabled', 0)->get());
    }

    /**
     * @SWG\Get(
     *     path="/front/profile/label",
     *     tags={"用户相关"},
     *     summary="用户标签",
     *     description="用户标签",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Response(
     *          response="200",
     *          description="调用成功",
     *          @SWG\Schema(
     *              @SWG\Property(
     *                  property="set",
     *                  description="设置过的标签",
     *                  type="array",
     *                  @SWG\Items(
     *                      @SWG\Property(property="id", type="integer", description="标签id"),
     *                      @SWG\Property(property="name", type="string", description="标签名"),
     *                  )
     *              ),
     *              @SWG\Property(
     *                  property="unset",
     *                  description="未设置的标签",
     *                  type="array",
     *                  @SWG\Items(
     *                      @SWG\Property(property="id", type="integer", description="标签id"),
     *                      @SWG\Property(property="name", type="string", description="标签名"),
     *                  )
     *              )
     *          )
     *      )
     * )
     */
    public function userLabel()
    {
        $data = [
            'set' => [],
            'unset' => []
        ];
        $ul = DB::table('user_label')->where('user_id', Auth::id())->first();
        if (is_null($ul)) {
            $data['unset'] = DB::table('article_label')->select('id', 'name')->where('is_enabled', 0)->get();
            return $this->renderJson($data);
        }
        $ids = explode(',', $ul->label);
        $data['set'] = DB::table('article_label')->select('id', 'name')->where('is_enabled', 0)->whereIn('id', $ids)->get();
        $data['unset'] = DB::table('article_label')->select('id', 'name')->where('is_enabled', 0)->whereNotIn('id', $ids)->get();
        return $this->renderJson($data);
    }

    /**
     * @SWG\Post(
     *     path="/front/profile/label",
     *     tags={"用户相关"},
     *     summary="添加标签",
     *     description="添加标签",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="label", in="formData", required=true, type="string", description="标签id,逗号分隔:'1,2,3'"),
     *     @SWG\Response(
     *          response="200",
     *          description="调用成功",
     *      )
     * )
     */
    public function userAddLabel()
    {
        $label = request('label');
        DB::table('user_label')->updateOrInsert(['user_id'=>Auth::id()],['label'=>$label, 'created_at'=>Carbon::now()]);
        return $this->success();
    }
}
